package com.lzxuni.modules.informationpublish;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.informationpublish.entity.InformationPublishPressRelease;
import com.lzxuni.modules.informationpublish.service.InformationPublishPressReleaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.sql.SQLException;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fcd
 * @since 2019-06-25
 */
@RestController
@RequestMapping("/informationPublishPressRelease")
public class InformationPublishPressReleaseController extends BaseController {
    @Autowired
    private InformationPublishPressReleaseService informationPublishPressReleaseService;

    @Autowired
    private FileEntityService fileEntityService;

    @RequestMapping("/Index")
    @SysLog(categoryId = 2, module = "信息发布-新闻公告", operateType = "访问")
    public ModelAndView list(){
        ModelAndView mv = new ModelAndView("informationpublish/pressrelease/index");
        return mv;
    }
    //信息发布-新闻公告的查询
    @RequestMapping("/GetList")
    public Object GetList(String pagination, InformationPublishPressRelease informationPublishPressRelease, String Keyword) throws SQLException {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        informationPublishPressRelease.setNewsTitle(Keyword);
        PageData pageData = getPageData(informationPublishPressReleaseService.queryPage(pageParameter, informationPublishPressRelease));
        return R.ok().put("data", pageData);
    }

    @RequestMapping("/Form")
    public ModelAndView insert() throws Exception{
        ModelAndView mv = new ModelAndView("informationpublish/pressrelease/Insert");
        return mv;
    }
    //添加、修改
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue, String demopics,InformationPublishPressRelease informationPublishPressRelease) throws Exception{
        if(StringUtils.isEmpty(keyValue)){
            String ywId = UuidUtil.get32UUID();
            informationPublishPressRelease.setCreateTime(new Date());
            informationPublishPressRelease.setNewsId(ywId);
            String type="news";
            informationPublishPressRelease.setNewsType(type);
            informationPublishPressReleaseService.save(informationPublishPressRelease);
            if(!StringUtils.isEmpty(demopics)&& !"&amp;nbsp;".equals(demopics)) {
                fileEntityService.insert(demopics.replace("&quot;", "\""), ywId, "tp", "information-news", null);
            }
            return R.ok("保存成功");
        }else{
            fileEntityService.deleteByYwId(keyValue);
            informationPublishPressReleaseService.updateById(informationPublishPressRelease);
            if(!StringUtils.isEmpty(demopics) && !"&amp;nbsp;".equals(demopics)) {
                fileEntityService.insert(demopics.replace("&quot;", "\""), keyValue, "tp", "information-news", null);
                informationPublishPressRelease.setNewsId(keyValue);
            }
            return R.ok("修改成功");
        }
    }
    //图片回显
    @RequestMapping("/Update")
    public ModelAndView update(String ywId,String keyValue) throws Exception{
        ModelAndView mv = new ModelAndView("informationpublish/pressrelease/Update");
        FileEntity fileBeanCustom = new FileEntity();
        fileBeanCustom.setYwId(ywId);
        fileBeanCustom.setYwType("information-news");
        mv.addObject("picNum", fileEntityService.queryNumByFileEntity(fileBeanCustom));
        return mv;
    }
    @RequestMapping("/DeleteForm")
    @SysLog(categoryId = 3,module = "信息发布-新闻公告",operateType = "删除")
    public Object delete(String keyValue) {
        informationPublishPressReleaseService.removeById(keyValue);
        fileEntityService.deleteByYwId(keyValue);
        return R.ok("删除成功");
    }

    @RequestMapping("GetEntity")
    public Object  GetEntity(String keyValue){
        InformationPublishPressRelease informationPublishPressRelease = informationPublishPressReleaseService.getById(keyValue);
        return R.ok().put("data",informationPublishPressRelease);
    }




//曝光台controller




    @RequestMapping("/Indexbgt")
    @SysLog(categoryId = 2, module = "信息发布-曝光台", operateType = "访问")
    public ModelAndView lists(){
        ModelAndView mv = new ModelAndView("informationpublish/pressrelease/indexbgt");
        return mv;
    }
    //信息发布-曝光台的查询
    @RequestMapping("/GetListbgt")
    public Object GetLists(String pagination, InformationPublishPressRelease informationPublishPressRelease, String Keyword) throws SQLException {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        informationPublishPressRelease.setNewsTitle(Keyword);
        PageData pageData = getPageData(informationPublishPressReleaseService.queryPages(pageParameter, informationPublishPressRelease));
        return R.ok().put("data", pageData);
    }

    @RequestMapping("/Formbgt")
    public ModelAndView inserts() throws Exception{
        ModelAndView mv = new ModelAndView("informationpublish/pressrelease/Insertbgt");
        return mv;
    }
    //添加、修改
    @RequestMapping("/SaveFormbgt")
    public Object insertDos(String keyValue, String demopics,InformationPublishPressRelease informationPublishPressRelease) throws Exception{
        if(StringUtils.isEmpty(keyValue)){
            String ywId = UuidUtil.get32UUID();
            informationPublishPressRelease.setCreateTime(new Date());
            informationPublishPressRelease.setNewsId(ywId);
            String type="bgt";
            informationPublishPressRelease.setNewsType(type);
            informationPublishPressReleaseService.save(informationPublishPressRelease);
            if(!StringUtils.isEmpty(demopics)&& !"&amp;nbsp;".equals(demopics)) {
                fileEntityService.insert(demopics.replace("&quot;", "\""), ywId, "tp", "information-bgt", null);
            }
            return R.ok("保存成功");
        }else{
            fileEntityService.deleteByYwId(keyValue);
            informationPublishPressReleaseService.updateById(informationPublishPressRelease);
            if(!StringUtils.isEmpty(demopics) && !"&amp;nbsp;".equals(demopics)) {
                fileEntityService.insert(demopics.replace("&quot;", "\""), keyValue, "tp", "information-bgt", null);
                informationPublishPressRelease.setNewsId(keyValue);
            }
            return R.ok("修改成功");
        }
    }
    //图片回显
    @RequestMapping("/Updatebgt")
    public ModelAndView updates(String ywId,String keyValue) throws Exception{
        ModelAndView mv = new ModelAndView("informationpublish/pressrelease/Updatebgt");
        FileEntity fileBeanCustom = new FileEntity();
        fileBeanCustom.setYwId(ywId);
        fileBeanCustom.setYwType("information-bgt");
        mv.addObject("picNum", fileEntityService.queryNumByFileEntity(fileBeanCustom));
        return mv;
    }
    @RequestMapping("/DeleteFormbgt")
    @SysLog(categoryId = 3,module = "信息发布-曝光台",operateType = "删除")
    public Object deletes(String keyValue) {
        informationPublishPressReleaseService.removeById(keyValue);
        fileEntityService.deleteByYwId(keyValue);
        return R.ok("删除成功");
    }

    @RequestMapping("GetEntitybgt")
    public Object  GetEntitys(String keyValue){
        InformationPublishPressRelease informationPublishPressRelease = informationPublishPressReleaseService.getById(keyValue);
        return R.ok().put("data",informationPublishPressRelease);
    }


}

